package com.yonyou.pmclouds.plantemplate.processor;

import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.plantemplate.entity.ScheduleTemplateItemVO;
import com.yonyou.pmclouds.plantemplate.entity.ScheduleTemplateVO;
import com.yonyou.pmclouds.plantemplate.rmiitf.IScheduleTemplateItemProcessor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Description: 计划模板任务节点处理类
 * @Author: zhangzhonghao
 * @Date: 2020-03-06
 */

@Slf4j
@Component
public class ScheduleTemplateItemInsertDefaultValueProcessor implements IScheduleTemplateItemProcessor {

    private List<ScheduleTemplateItemVO> insertScheduleItemList = new ArrayList<>();

    private List<ScheduleTemplateItemVO> updateScheduleItemList = new ArrayList<>();

    private Map<String, String> codeWithPKMap = new HashMap<>();

    @Override
    public void process(ScheduleTemplateVO templateVO, ScheduleTemplateItemVO rootParentItemVO, ScheduleTemplateItemVO parentItemVO,
                        ScheduleTemplateItemVO childItemVO) throws BusinessException {

        boolean isInsert = StringUtils.isEmpty(childItemVO.getPkScheduleTemplateItem());

        if(isInsert){
            insertScheduleItemList.add(childItemVO);
            setInsertDefault(templateVO, rootParentItemVO, parentItemVO, childItemVO);
        }else {
            updateScheduleItemList.add(childItemVO);
            if(StringUtils.isEmpty(childItemVO.getDependPlanName())){
                childItemVO.setDependPlan("");
            }
        }

        codeWithPKMap.put(childItemVO.getCode(), childItemVO.getPkScheduleTemplateItem());

        childItemVO.setChildCount(childItemVO.getChildren() == null ? 0 : childItemVO.getChildren().length);
    }

    private void setInsertDefault(ScheduleTemplateVO templateVO, ScheduleTemplateItemVO rootParentItemVO, ScheduleTemplateItemVO parentItemVO,
                                  ScheduleTemplateItemVO childItemVO){
        String pkScheduleItem = UUID.randomUUID().toString();
        childItemVO.setPkScheduleTemplateItem(pkScheduleItem);
        childItemVO.setPkScheduleTemplate(templateVO.getPkScheduleTemplate());

        if(parentItemVO == null){
            // 根结点
            childItemVO.setPkRoot(pkScheduleItem);
        }else {
            childItemVO.setPkRoot(rootParentItemVO.getPkScheduleTemplateItem());
            childItemVO.setPkParent(parentItemVO.getPkScheduleTemplateItem());
        }

        if(StringUtils.isNotBlank(childItemVO.getCode())){
            String[] codes = childItemVO.getCode().split("\\.");
            if(codes.length > 0) childItemVO.setSeq(Integer.parseInt(codes[codes.length - 1]));
        }
    }





    public List<ScheduleTemplateItemVO> getInsertScheduleItemList() {
        return insertScheduleItemList;
    }

    public List<ScheduleTemplateItemVO> getUpdateScheduleItemList() {
        return updateScheduleItemList;
    }

    public Map<String, String> getCodeWithPKMap() {
        return codeWithPKMap;
    }
}
